<template>
  <div class="container">
    <el-card>
      <el-row>
        <el-col :span="12">
          <div class="sousuo" style="margin-left: 30px">
            关键字
            <el-input
              v-model="keyword"
              placeholder="根据编号搜索"
              style="width: 400px"
            ></el-input>
          </div>
        </el-col>
        <el-col :span="12" primary>
          <div class="button" style="text-align: right">
            <el-button @click="clickDel">清除</el-button>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-search"
              @click="clickSearch()"
              >搜索</el-button
            >
          </div>
        </el-col>
      </el-row>
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        style="margin-top: 20px"
      >
      </el-alert>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="编号" width="250"> </el-table-column>
        <el-table-column
          prop="questionType"
          label="题型"
          :formatter="formatterMathed"
        >
        </el-table-column>
        <el-table-column prop="questionIDs" label="题目编号" width="200">
          <template #default="{ row }">
            <el-link
              type="primary"
              v-for="item in row.questionIDs"
              :key="item.number"
              @click="openDialog(item)">{{ item.number }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="250">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)" width="250">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)" width="250">
        </el-table-column>
        <el-table-column prop="userName" label="录入人" width="250">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="handleDel(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog :visible="showDialog" title="题目预览" @close="closeDialog">
      <el-descriptions>
        <el-descriptions-item label="【题型】" prop="questionType">
          <span>{{ myData.questionType |tx}}</span>
        </el-descriptions-item>
        <el-descriptions-item label="【编号】">{{
          myData.id
        }}</el-descriptions-item>
        <el-descriptions-item label="【难度】">{{myData.difficulty|nd}}</el-descriptions-item>
        <el-descriptions-item label="【标签】">{{
          myData.tags
        }}</el-descriptions-item>
        <el-descriptions-item label="【学科】">{{
          myData.subjectID
        }}</el-descriptions-item>
        <el-descriptions-item label="【目录】">{{
          myData.catalogID
        }}</el-descriptions-item>
        <el-descriptions-item label="【方向】">{{
          myData.direction
        }}</el-descriptions-item>
      </el-descriptions>
      <el-divider></el-divider>
        【题干】：<div v-html="myData.question" style="color:blue"></div>
              <div v-if="myData.questionType!=='3'">
        <div style="padding-bottom:5px">{{myData.questionType|tx}} 选项：（以下选中的选项为正确答案）</div>
        <div v-for="item in myData.options" :key="item.code" style="padding:8px 0">
          <el-radio v-if="myData.questionType==='1'" :value="item.isRight" :label="1">{{item.title}}</el-radio>
          <el-checkbox v-if="myData.questionType==='2'" :value="item.isRight?true:false" >{{item.title}}</el-checkbox>
        </div>
      </div>
      <el-divider></el-divider>
      【参考答案】：<el-button type="danger" size="small" @click="preivewVideo()">视频答案预览</el-button>
      <div class="video" v-show="play">
        <video ref="video" :src="myData.videoURL" controls></video>
      </div>
      <el-divider></el-divider>
      【答案解析】：<div style="display:inline-block" v-html="myData.answer"></div>
      <el-divider></el-divider>
      <el-descriptions>
        <el-descriptions-item label="【题目备注】">{{
          myData.remarks
        }}</el-descriptions-item>
      </el-descriptions>
    <div style="text-align: right">
            <el-button @click="closeDialog" type="primary" size="small"
        >关闭</el-button
      >
    </div>
    </el-dialog>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import { questionType, difficulty } from '@/api/hmmm/constants'

export default {

  data () {
    return {
      myData: {},
      questList: [],
      tableData: [],
      total: 0,
      page: 1,
      pagesize: 5,
      keyword: null,
      list: [],
      value: '',
      showDialog: false,
      play: false,
      detailId: ''
    }
  },
  filters: {
    nd (value) {
      if (value) return difficulty.find(item => item.value === +value).label
    },
    tx (value) {
      if (value) return questionType.find(item => item.value === +value).label + '题'
    }
  },
  created () {
    this.getRandomList()
  },
  methods: {
    async getRandomList () {
      const {
        data: { items, counts }
      } = await randoms({
        page: this.page,
        pagesize: this.pagesize,
        keyword: this.keyword
      })
      console.log('-------获取组题列表-----', items)
      this.tableData = items
      this.total = counts
    },

    // 每页有几条
    handleSizeChange (val) {
      this.pagesize = val
      // console.log('---------', this.pagesize)
      this.getRandomList()
    },
    // 当前第几页
    handleCurrentChange (val) {
      this.page = val
      this.getRandomList()
    },
    // 清除
    clickDel () {
      this.keyword = null
    },
    // 搜索
    clickSearch () {
      this.page = 1
      this.getRandomList()
    },
    // 格式化题型
    formatterMathed ({ questionType: QuestionType }) {
      const obj = questionType.find((item) => item.value === +QuestionType)
      return obj ? obj.label : '未知'
    },
    // 删除对应的列表
    handleDel (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
        .then(async () => {
          // 将id转化为真正的字符串
          await removeRandoms(JSON.stringify(id))
          // console.log('-----删除对应的列表-----', res)
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          this.getRandomList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    // 关闭对话框
    closeDialog () {
      this.play = false
      this.showDialog = false
      this.$refs.video.pause()
    },
    // 基础题库详情
    async openDialog (data) {
      console.log('---------', data.id)
      this.showDialog = true
      const { data: res } = await detail({ id: data.id })
      console.log('----基础题库详情--', res)
      this.myData = res
    },
    // 视频播放
    preivewVideo () {
      this.play = true
      this.$refs.video.play()
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  .pagination {
    margin: 20px 0;
    float: right;
  }
  .button-close{
    margin: 20px;
    float: right;
  }
}
</style>
